//
//  AppDelegate.swift
//  myBlog
//
//  Created by mac2014 on 16/3/20.
//  Copyright © 2016年 AD. All rights reserved.
//

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        //设置tabbar控制器
        //let tabBarVC = ADMainController()
        //创建window
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //创建根控制器
        window?.rootViewController = defaultViewController()
        
        //用于调试开发页面
        //window?.rootViewController = ADHomeViewController()
        
        window?.rootViewController?.view.backgroundColor = UIColor.whiteColor()
        
        window?.makeKeyAndVisible()
	
//        let homeVC = ADHomeViewController()
//        homeVC.title = "首页"
//        homeVC.tabBarItem.image = UIImage(named: "tabbar_home")
//        tabBarVC.addChildViewController(UINavigationController(rootViewController: homeVC))
        
        return true
    }
    
    //界面登录跳转
    //应用程序打开时进行判断
    func defaultViewController() -> UIViewController {
        //如果没有登录，则跳转到mainViewController
        if !ADUserAcountViewModel.sharedInstance.isLogin {
            return ADMainController()
        }
        //登录过：有新版本则跳转到新特性界面，否则到欢迎界面
        return isNewVersion() ? ADNewFeatureController() : ADWelComeController()
    }
    
    //控制器的跳转
    func jumpToRootViewController(controller: UIViewController) {
        window?.rootViewController = controller
    }
    
    //切换根控制器
    class func jumpToSpecifiedViewController(controller: UIViewController) {
        //让AppDelegate统一管理界面的跳转
        let appDelegate = UIApplication.sharedApplication().delegate as? AppDelegate
        appDelegate?.jumpToRootViewController(controller)
    }

    
    //判断当前版本是否是新版本
    func isNewVersion() -> Bool {
        //得到当前版本信息
        let version = NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String
        //上一次版本
        let sandboxKey = "sandboxKey"
        let sandboxVersion = NSUserDefaults.standardUserDefaults().stringForKey(sandboxKey)
        //对比:如果当前版本和上一个版本不相同，则当前版本为最新版本
        let newestVersion = version != sandboxVersion
        
        //保存最新版本号
        if newestVersion {
            NSUserDefaults.standardUserDefaults().setValue(version, forKey: sandboxKey)
            //系统会保存到该应用下的/Library/Preferences/gongcheng.plist文件中。需要注意的是如果程序意外退出，NSUserDefaultsstandardUserDefaults数据不会被系统写入到该文件，所以，要使用［[NSUserDefaultsstandardUserDefaults] synchronize］命令直接同步到文件里，来避免数据的丢失。
            NSUserDefaults.standardUserDefaults().synchronize()
        }

        //返回最新版本
        return newestVersion
        
    }

    func applicationWillResignActive(application: UIApplication) {
        // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
        // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
    }

    func applicationDidEnterBackground(application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(application: UIApplication) {
        // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }


}

